/**
*Proyecto: Autopista V.1
*CustomUserDetailsService.java
*Fecha Creacion: 6 Julio del 2013
*SWITZ Software Artists Derechos Reservados R
*/
package mx.com.switz.autopista.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import mx.com.switz.autopista.dao.UsuarioDao;
import mx.com.switz.autopista.dto.Usuario;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class CustomUserDetailsService implements UserDetailsService {
	/**
	*Clase que permite el acceso a un usuario si esxiste, y determinar el tipo de rol que tiene
	*@author Hector Silvar SWITZ Software Artists
	*/

	@Autowired
	UsuarioDao usuarioDao;
	/**
	 * Metodo que permite la validacion de un usuario, asi como el tipo de rol al que pertenece
	 */
	public UserDetails loadUserByUsername(String nombre)
			throws UsernameNotFoundException {

		// Declare a null Spring User
		UserDetails user = null;
		// TODO Auto-generated method stub

		try {

			// Search database for a user that matches the specified username
			// You can provide a custom DAO to access your persistence layer
			// Or use JDBC to access your database
			// DbUser is our custom domain user. This is not the same as
			// Spring's User
			Usuario u = (Usuario) usuarioDao.selectUsuario(nombre).get(0);
			System.out.println("u  " + u.getNombre());

			// Populate the Spring User object with details from the dbUser
			// Here we just pass the username, password, and access level
			// getAuthorities() will translate the access level to the correct
			// role type
			user = new User(u.getNombre(), u.getPassword(), true, true, true,
					true, getAuthorities(1));
			;

		} catch (Exception e) {

			throw new UsernameNotFoundException("Error in retrieving user");
		}

		// Return user to Spring for processing.
		// Take note we're not the one evaluating whether this user is
		// authenticated or valid
		// We just merely retrieve a user that matches the specified username
		return user;
	}
	/**
	 * Metodo que permite asignar roles de usuario segun un usuario
	 * @param access
	 * @return
	 */
	public Collection<GrantedAuthority> getAuthorities(Integer access) {
		// Create a list of grants for this user
		List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);

		if (access.compareTo(1) == 0) {
			// User has admin access

			authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
		} else if (access.compareTo(0) == 0) {

			authList.add(new GrantedAuthorityImpl("ROLE_USER"));
		}

		// Return list of granted authorities
		return authList;
	}

}
